import axios from 'axios'
// import { Toast } from 'vant';
// import "vant/es/toast/style";
// import { useRouter } from 'vue-router'
// const instance = axios.create({
//     baseURL: 'https://www.fastmock.site/mock/317cadf56935626bac3b5456bf59a0a4/api/',
//     timeout: 3000
// })
const instance = axios.create({
    baseURL: 'https://api.bhmanhua.top/api/manage/',
    timeout: 60000
})
// const instance = axios.create({
//     baseURL: 'http://test.fanyi.bhmanhua.top/api/',
//     timeout: 10000
// })
// const router = useRouter()
//http response 封装后台返回拦截器
instance.interceptors.response.use(
    response => {
        //当返回信息为未登录或者登录失效的时候重定向为登录页面
        if (response.data.errno === "LT400") {
            const promise = new Promise((resolve) => {
                // setTimeout(() => {
                //     resolve(Toast.fail('登录超时,请重新登录'))
                // }, 1000)
            })
            promise.then(() => {
                window.location.href = "#/login"
                localStorage.removeItem('token')
                localStorage.removeItem('userInfo')
            })
            // router.push('/login')
        }
        return response;
    },
    error => {
        return Promise.reject(error)
    }
)
// 请求拦截，在每次请求中的 header 中带上 token
instance.interceptors.request.use(config => {
    let token = localStorage.getItem('token')
    if (token) {
        config.headers.common.token = token
    }
    return config
}, error => {
    return Promise.reject(error);
})

export const get = ((url, params = {}) => {
    return new Promise((resolve, reject) => {
        instance.get(url, { params }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
})

export const post = ((url, data = {}) => {
    return new Promise((resolve, reject) => {
        instance.post(url, data).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
})
export const patch = ((url, data = {}, headers = { 'content-type': 'multipart/form-data' }) => {
    return new Promise((resolve, reject) => {
        instance.patch(url, data, headers).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
})
export const upload = ((url, data = {}, headers = { 'content-type': 'multipart/form-data' }) => {
    return new Promise((resolve, reject) => {
        instance.post(url, data, headers).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
})